package tree;

import java.util.ArrayList;
import java.util.List;

public class BinarySearchTreeIterator173 {

        private int idx;
        private List<Integer> arr;

        public BinarySearchTreeIterator173(TreeNode root) {
            idx = 0;
            arr = new ArrayList<Integer>();
            inorderTraversal(root, arr);
        }

        public int next() {
            return arr.get(idx++);
        }

        public boolean hasNext() {
            return idx < arr.size();
        }

        private void inorderTraversal(TreeNode root, List<Integer> arr) {
            if (root == null) {
                return;
            }
            inorderTraversal(root.left, arr);
            arr.add(root.val);
            inorderTraversal(root.right, arr);
        }

    public static class FlattenBinaryTreeToLinkedList114 {
        public void flatten(TreeNode root) {
            List<TreeNode> list = new ArrayList<>();
            preOrder(root,list);
            for (int i = 1; i < list.size(); i++) {
                TreeNode node = list.get(i-1);
                node.left =null;
                node.right = list.get(i);
            }
        }

        private void preOrder(TreeNode root, List<TreeNode> list) {
            if(root==null){
                return;
            }
            list.add(root);
            preOrder(root.left,list);
            preOrder(root.right,list);
        }
    }
}
